package cn.edu.bupt.freewings.forum.front.utility;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.bupt.freewings.forum.common.PostType;
import cn.edu.bupt.freewings.forum.front.beans.PostBean;
import cn.edu.bupt.freewings.forum.front.Constants;
import cn.edu.bupt.freewings.forum.front.beans.UserBean;

/**
 * @author hubaiyu (hubaiyu@gmail.com)
 * 
 */
public class CommonUtility {
	public static List<PostBean> postListToPostBeanList(PostType.PostList postList) {
		List<PostBean> posts = new ArrayList<PostBean>();
		for (PostType.Post post : postList.getPostList())
			posts.add(new PostBean(post));
		return posts;
	}

	public static UserBean getUser(HttpServletRequest request,
			HttpServletResponse response) {
		return (UserBean) request.getSession().getAttribute(Constants.USER);
	}
	
	public static Object getValue(HttpServletRequest request, String name){
		Object value = request.getParameter(name);
		if(value != null)
			return value;
		value = request.getAttribute(name);
		if(value != null)
			return value;
		value = request.getSession().getAttribute(name);
		if(value != null)
			return value;
		return value;
	}
	
	public static int getParamIntValue(HttpServletRequest request, String name){
		return Integer.valueOf(request.getParameter(name)).intValue();
	}
}
